// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.Threading.Tasks;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace IdentityServerHost.Pages.Ciba
{
    [SecurityHeaders]
    public class IndexModel : PageModel
    {
        public BackchannelUserLoginRequest LoginRequest { get; set; }

        private readonly IBackchannelAuthenticationInteractionService _backchannelAuthenticationInteraction;
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(IBackchannelAuthenticationInteractionService backchannelAuthenticationInteractionService, ILogger<IndexModel> logger)
        {
            _backchannelAuthenticationInteraction = backchannelAuthenticationInteractionService;
            _logger = logger;
        }

        public async Task<IActionResult> OnGet(string id)
        {
            LoginRequest = await _backchannelAuthenticationInteraction.GetLoginRequestById(id);
            if (LoginRequest == null)
            {
                _logger.LogWarning("Invalid backchannel login id {id}", id);
                return RedirectToPage("/home/error/index");
            }

            return Page();
        }
    }
}
